package com.example.BD;

import java.util.ArrayList;

import com.example.metier.Contact;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ContactsDBAdaptateur {
	private static final int BASE_VERSION = 1;
	private static final String BASE_NOM = "contacts.db";
	private static final String tContact = "tContact";
	private static final String cNom = "NOM";
	private static final int cNomID = 0;
	private static final String cPrenom = "PRENOM";
	private static final int cPrenomID = 1;
	private static final String cNum = "NUM";
	private static final int cNumID = 2;

	// La requête de création de la structure de la base de données.
	private static final String reqTC = "create table " + tContact + " ("
			+ cNom + " text primary key , " + cPrenom + " text , " + cNum
			+ " text not );";

	// L’instance de la base qui sera manipulée au travers de cette classe.

	private SQLiteDatabase maBaseDonnees;
	private MaBD baseHelper;

	public ContactsDBAdaptateur(Context ctx) {
		baseHelper = new MaBD(ctx, BASE_NOM, null, BASE_VERSION);
	}

	/**
	 * Ouvre la base de données en écriture.
	 */
	public SQLiteDatabase open() {
		maBaseDonnees = baseHelper.getWritableDatabase();
		return maBaseDonnees;
	}

	/**
	 * Ferme la base de données.
	 */
	public void close() {
		maBaseDonnees.close();
	}

	public SQLiteDatabase getBaseDonnees() {
		return maBaseDonnees;
	}

	public Contact getContact(String nom) {

		Cursor c = maBaseDonnees.query(tContact, new String[] { cNom, cPrenom,
				cNum }, null, null, null, cNom + " LIKE " + nom, null);
		return cursorToContact(c);

	}

	private Contact cursorToContact(Cursor c) {
		if (c.getCount() == 0)
			return null;
		Contact ct = new Contact();
		// Extraction des valeurs depuis le curseur.
		ct.setNom(c.getString(cNomID));
		ct.setPrenom(c.getString(cPrenomID));
		ct.setNum(c.getString(cNumID));
		// Ferme le curseur pour libérer les ressources.
		c.close();
		return ct;

	}

	public ArrayList<String> getAllNomContacts() {
		Cursor c = maBaseDonnees.query(tContact, new String[] { cNom, cPrenom,
				cNum }, null, null, null, null, null);
		return cursorToContacts(c);

	}

	private ArrayList<String> cursorToContacts(Cursor c) {
		if (c.getCount() == 0)
			return new ArrayList<String>();

		ArrayList<String> listNom = new ArrayList<String>(c.getCount());
		c.moveToFirst();
		do {
			String nom = c.getString(cNomID) + " " + c.getString(cPrenomID)
					+ " : " + c.getShort(cNumID);
			listNom.add(nom);
		} while (c.moveToNext());
		// Ferme le curseur pour libérer les ressources.
		c.close();
		return listNom;

	}

	public void insertContact(Contact c) {
		ContentValues val = new ContentValues();
		val.put(cNom, c.getNom());
		val.put(cPrenom, c.getPrenom());
		val.put(cNum, c.getNum());
		maBaseDonnees.insert(tContact, null, val);
	}
}